package net.lqy.aipan.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import net.lqy.aipan.dto.AccountDTO;
import net.lqy.aipan.enums.BizCodeEnum;
import net.lqy.aipan.util.CommonUtil;
import net.lqy.aipan.util.JsonData;
import net.lqy.aipan.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.protocol.HTTP;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @title: LoginInterceptor
 * @Author lqy
 * @Date: 2025-06-03 21:33
 * @Version 1.0
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    public static  ThreadLocal<AccountDTO> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //处理options请求类型
        if (request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) {
            response.setStatus(HttpStatus.NO_CONTENT.value());
            return true;
        }else {
            String token = request.getHeader("token");
            if (token==null|| StringUtils.isBlank(token)){
                token=request.getParameter("token");
            }
            //解析token
            if (StringUtils.isNotBlank(token)){
                Claims claims = JwtUtil.checkLoginJWT(token);
                if (claims!=null){
                    AccountDTO accountDTO = new AccountDTO();
                    accountDTO.setId(claims.get("accountId",Long.class));
                    accountDTO.setUsername(claims.get("username",String.class));
                    accountDTO.setPhone(claims.get("phone",String.class));
                    accountDTO.setRole(claims.get("role",String.class));
                    threadLocal.set(accountDTO);
                    return true;
                }else {
                    log.error("token解析失败");
                    CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
                    return false;
                }
            }else {
                log.error("token为空");
                CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
                return false;
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        threadLocal.remove();
    }
}
